import axios from 'axios'
import Cookies from 'js-cookie'



axios.defaults.baseURL='http://127.0.0.1:8000/';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
if (Cookies.get('token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
  axios.defaults.headers.common['Authorization'] = `JWT ${Cookies.get('token')}`;
}



//用户注册
export const register= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/user/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//用户登录
export const login= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/login/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//获取用户个人信息
export const getUser= () => {
  return new Promise((resolve, reject) => {
    axios.get('/api/user/1/',).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//获取指定用户的信息
export const getSpecialUser= (id) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/user/?id='+ id,).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//更新用户个人信息
export const updateUser= (parms) => {
  return new Promise((resolve, reject) => {
    axios.put('/api/user/1/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//推荐用户
export const getHotUser= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/userHot/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//搜索用户
export const getSearchUser= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/user/?search=' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};
